﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK.Graphics;
using System.Drawing;
using OpenTK;
using DataModel.Shapes;
using System.Threading;

namespace VirtualEnvironmentWPF
{
    public class TestGame : GameWindow
    {
        private SimpleShape shape;
        private float angle=0;
        private float x=0, y=0, z=0;
        private float step = 0.1f;

        public TestGame()
        {
            shape = new Cube(1, 2, 3, Color.DarkOrange, 0, 0, 2);
            //shape = new Pyramid(1, 1, 1, Color.DarkOrange, 0, 0, 0);
        }
        
        
        
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            GL.ClearColor(Color.MidnightBlue);
            GL.Enable(EnableCap.DepthTest);
        }

        /// <summary>
        /// Called when the user resizes the window.
        /// </summary>
        /// <param name="e">Contains the new width/height of the window.</param>
        /// <remarks>
        /// You want the OpenGL viewport to match the window. This is the place to do it!
        /// </remarks>
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            GL.Viewport(0, 0, Width, Height);

            double aspect_ratio = Width / (double)Height;

            OpenTK.Matrix4 perspective = OpenTK.Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)aspect_ratio, 1, 64);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref perspective);
        }

       
        /// <summary>
        /// Prepares the next frame for rendering.
        /// </summary>
        /// <remarks>
        /// Place your control logic here. This is the place to respond to user input,
        /// update object positions etc.
        /// </remarks>
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);

            if (Keyboard[OpenTK.Input.Key.Escape])
            {
                this.Exit();
                return;
            }

            if (Keyboard[OpenTK.Input.Key.Q])
                  angle += 5;
            else if (Keyboard[OpenTK.Input.Key.E])
                angle -= 5;
            else if (Keyboard[OpenTK.Input.Key.W])
                y += step;
            else if (Keyboard[OpenTK.Input.Key.S])
                y -= step;
            else if (Keyboard[OpenTK.Input.Key.A])
                x -= step;
            else if (Keyboard[OpenTK.Input.Key.D])
                x += step;
            else if (Keyboard[OpenTK.Input.Key.R])
                z -= step;
            else if (Keyboard[OpenTK.Input.Key.F])
                z += step;

        }
                
        /// <summary>
        /// Place your rendering code here.
        /// </summary>
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            Matrix4 lookat = Matrix4.LookAt(Vector3.UnitZ, Vector3.Zero, Vector3.UnitY);

            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref lookat);

            //angle += rotation_speed * (float)e.Time;
            GL.Rotate(angle, 0f, y, z);

            shape.UpdateVertices(x, y, z);
            shape.DrawShape();

            this.SwapBuffers();
            Thread.Sleep(1);
        }
    }
}
